package cn.anosi.repository.auth;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import cn.anosi.entity.auth.UserRole;

public interface UserRoleRepository extends JpaRepository<UserRole, Integer>,JpaSpecificationExecutor<UserRole>{

	/**
	 * 查询用户的角色id
	 * @param id
	 * @return
	 */
	@Query(value="select role_id from user_role where user_id = ?" , nativeQuery = true)
	List<Integer> roleIds(Integer id);
	
	/**
	 * 删除用户关联的角色
	 * @param roleId
	 * @return
	 */
	@Modifying
	@Query(value = "delete FROM user_role where user_id = ?", nativeQuery=true)
	int deleteRoleByUserId(Integer roleId);

	/**
	 * 用户关联角色
	 * @param roleId
	 * @param id
	 * @return
	 */
	@Modifying
	@Query(value = "insert into user_role (user_id, role_id) values (?1, ?2)",nativeQuery=true)
	int associate(Integer userId, Integer roleId);
}
